/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.pitkley.jocabulary;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;

/**
 *
 * @author pit
 */
public class AskUnits {

    private LinkedList<Unit> units = new LinkedList<Unit>();
    private LinkedList<Vocable> vocab = new LinkedList<Vocable>();
    private int startSize = 0;
    private int askedCount = 0;

    public AskUnits(LinkedList<Unit> pUnits, int pTimesCorrect, boolean pRequired, boolean pRandomAskOrder) {
        init(pUnits, pTimesCorrect, pRequired, pRandomAskOrder);
    }

    public AskUnits(List<Unit> pUnits, int pTimesCorrect, boolean pRequired, boolean pRandomAskOrder) {
        init(new LinkedList(pUnits), pTimesCorrect, pRequired, pRandomAskOrder);
    }

    public AskUnits(Unit[] pUnits, int pTimesCorrect, boolean pRequired, boolean pRandomAskOrder) {
        init(new LinkedList(Arrays.asList(pUnits)), pTimesCorrect, pRequired, pRandomAskOrder);
    }

    private void init(LinkedList<Unit> pUnits, int pTimesCorrect, boolean pRequired, boolean pRandomAskOrder) {
        units = pUnits;

        for (Unit u : units) {
            u.loadVocab();

            for (Vocable v : u.getVocab()) {
                if ((pTimesCorrect == 3) || (v.getTimesCorrect() <= pTimesCorrect)) {
                    if ((pRequired && v.getRequired()) || !pRequired) {
                        vocab.add(v);
                    }
                }
            }
        }

        Collections.shuffle(vocab);

        startSize = vocab.size();
        askedCount = 0;
    }

    public Vocable getNextVocable() throws NoSuchElementException {
        askedCount++;
        return vocab.pop();
    }

    public int getStartSize() {
        return startSize;
    }

    public int getAskedCount() {
        return askedCount;
    }

    public void writeFiles() {
        for (Unit u : units) {
            u.writeFile();
        }
    }
}
